package pl.makimo.fuzzy.norms;

import java.util.HashMap;

import pl.makimo.fuzzy.sets.CrispSet;
import pl.makimo.fuzzy.sets.DiscreteFuzzySet;
import pl.makimo.fuzzy.sets.Type2DiscreteFuzzySet;
import pl.makimo.fuzzy.sets.memberships.DiscreteMembershipFunction;

public class Type2MinMaxProductNorm extends Norm {
	public Norm norm1 = new MinNorm();
	public Norm norm2 = new MinNorm();
	
	public Type2MinMaxProductNorm() {
		
	}
	
	public Type2MinMaxProductNorm(Norm norm1, Norm norm2) {
		super();
		this.norm1 = norm1;
		this.norm2 = norm2;
	}

	@Override
	public Object value(Object a, Object b) {
		DiscreteFuzzySet set1 = (DiscreteFuzzySet)a;
		DiscreteFuzzySet set2 = (DiscreteFuzzySet)b;
		
		HashMap<Object, Object> mappings = new HashMap<Object, Object>();
		CrispSet<Double> newElements = new CrispSet<Double>();
		
		for (final Object element1 : set1.elements) {
			for (final Object element2 : set2.elements) {
				mappings.put(
							norm2.value(element1, element2),
							norm1.value(set1.value(element1), set2.value(element2))
						);
				
				newElements.add((double)(Double)norm2.value(element1, element2));
			}
		}
    	 
    	DiscreteFuzzySet newSet = new DiscreteFuzzySet(new DiscreteMembershipFunction(mappings));
    	newSet.elements = newElements;
    	
    	return newSet;		
	}
}
